package org.footballmanager.pandafootball.common.util;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.*;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.common.exception.ExceptionEnum;
import org.footballmanager.pandafootball.common.exception.MyException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.util.StringUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;

@Slf4j
public class SpiderUtil implements Runnable {
    private static final String TBODYTR = "tbody tr";
    //用于存放爬取页面的列表页面 转换成 list
	private List<String> allPageList = new ArrayList<>();
    //用于存放爬取球员信息的url
    private List<String> playerPageList = new ArrayList<>();

    private String baseUrl = "http://pesdb.net/pes2018";
    private static final boolean isGetImage = true;

    private Set<String> pos0 = new HashSet<>();
    private Set<String> pos1 = new HashSet<>();
    private Set<String> pos2 = new HashSet<>();

    private List<Person> personList = new ArrayList<>();

    private StringBuilder result;

	private SpiderUtil() {
		log.info("---------start-----------" + new Date().getTime());
		Thread thread = new Thread(this);
		thread.start();
		try {
			thread.join();
		} catch (InterruptedException e) {
			log.error(e.getMessage());
            Thread.currentThread().interrupt();
			throw new MyException(ExceptionEnum.INTERRUPTEDEXCEPTION, e);
		}
		log.info("----------end------------" + new Date().getTime());
	}

    /**
     *
     */
	@Override
	public void run() {
		processPage();
        int j = 1;
        if(allPageList == null || allPageList.isEmpty()) {
            return;
        }
        for (String url: allPageList) {
            log.info("Process Page:" + j++ + "/" + allPageList.size());
            repeartMethod(url, true);
        }
        j = 1;
        if(playerPageList == null || playerPageList.isEmpty()) {
            return;
        }
        for (String url: playerPageList) {
            log.info("Process Detail:" + j++ + "/" + playerPageList.size());
            repeartMethod(url, false);
        }
		exportToExcel(personList);
	}

	private void repeartMethod(String url, boolean isPage){
        try {
            Document doc = Jsoup.connect(url)
                    .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31")
                    .timeout(3000)
                    .get();
            if(isPage){
                getPlayerPageList(doc);
            }else{
                processPlayerList(doc, findIdByUrl(url));
            }
            Thread.sleep(1000);
        } catch (IOException e) {
            log.error("Jsoup IOError:" + e.getMessage());
//            repeartMethod(url, isPage);
            throw new MyException(ExceptionEnum.INPUTEXCEPTION, e);
        } catch (InterruptedException e) {
            log.info("Thread Error:" + e.getMessage());
            Thread.currentThread().interrupt();
            throw new MyException(ExceptionEnum.INTERRUPTEDEXCEPTION, e);
        }
    }

    private String findIdByUrl(String url) {
	    //http://pesdb.net/pes2018/?id=4522
	    return url.replaceAll("http://pesdb.net/pes2018/?id=", "");
    }

    private void processPlayerList(Document doc, String id){
	    if(isGetImage){
            //查找图像
            Element img = doc.select("table.player tr td img").first();
            if(img != null){
                String imgSrc = img.attr("src"); //获取src属性的值
                if(!StringUtils.isEmpty(imgSrc)){
                    processImage(baseUrl + "/" + imgSrc, id);
                }
            }
        }else{
            Elements tables = doc.select("table.player table");
            //成员基本信息
            Element row1 = tables.get(0);
            Person person = new Person();
            person.setId(id);
            processRow1(row1, person);
            //成员技能信息1
            Element row2 = tables.get(1);
            processRow2(row2, person);
            //成员技能信息2
            Element row3 = tables.get(2);
            processRow3(row3, person);
            //成员踢球风格
            Element row4 = tables.get(3);
            processRow4(row4, person);
            personList.add(person);
        }
    }

    private void processRow4(Element row4, Person person) {
        Elements trs = row4.select(TBODYTR);
        int times = 0;
        String style = "";
        String skills = "";
        String com = "";
        for(Element tr: trs){
            String td = tr.select("td").text();
            if(td == null){
                times = times + 1;
                continue;
            }
            if(times == 1){
                style += td + Constants.COMMA;
            }else if(times == 2){
                skills += td + Constants.COMMA;
            }else{
                com += td + Constants.COMMA;
            }
            person.setStyle(style);
            person.setSkills(skills);
            person.setCom(com);
        }
    }

    private void processRow3(Element row3, Person person) {
        Elements trs = row3.select(TBODYTR);
        for(int i = 0; i < 15; i++){
            String td = trs.get(i).select("td").last().text();
            if(i <= 7){
                processRow3Switch1To7(i, person, td);
            }else{
                processRow3Switch8To14(i, person, td, trs);
            }

        }
    }

    private void processRow3Switch8To14(int i, Person person, String td, Elements trs) {
        switch (i){
            case 8:
                person.setForm(td);
                break;
            case 9:
                person.setInjury(td);
                break;
            case 10:
                person.setWeakfootusage(td);
                break;
            case 11:
                person.setWeakfootaccuracy(td);
                break;
            case 12:
                break;
            case 13:
                person.setOverall(td);
                break;
            case 14:
                person.setRating(processRating(trs.get(i).select("th").first()));
                break;
            default:
                break;
        }
    }

    private void processRow3Switch1To7(int i, Person person, String td){
        switch (i){
            case 0:
                person.setPhysical(td);
                break;
            case 1:
                person.setJump(td);
                break;
            case 2:
                person.setStamina(td);
                break;
            case 3:
                person.setGoalkeeping(td);
                break;
            case 4:
                person.setCatching(td);
                break;
            case 5:
                person.setClearing(td);
                break;
            case 6:
                person.setReflexes(td);
                break;
            case 7:
                person.setCoverage(td);
                break;
            default:
                break;
        }
    }

    private Map<String,String> processRating(Element th) {
        Element select = th.getElementById("select_rating_as");
        Elements options = select.select("option");
        Map<String, String> map = new HashMap<>();
        for(Element option: options){
            Document doc = Jsoup.parse(option.val());
            map.put(option.text(),  doc.text());
        }
        return map;
    }

    private void processRow2(Element row2, Person person) {
        Elements trs = row2.select(TBODYTR);
        for(int i = 0; i < 15; i++){
            String td = trs.get(i).select("td").last().text();
            if(i <= 7){
                processRow2Switch1To7(i, person, td);
            }else{
                processRow2Switch8To14(i, person, td);
            }

        }
    }

    private void processRow2Switch1To7(int i, Person person, String td){
        switch (i){
            case 0:
                person.setAttacking(td);
                break;
            case 1:
                person.setBallcontrol(td);
                break;
            case 2:
                person.setDribbling(td);
                break;
            case 3:
                person.setLow(td);
                break;
            case 4:
                person.setLofted(td);
                break;
            case 5:
                person.setFinishing(td);
                break;
            case 6:
                person.setPlace(td);
                break;
            case 7:
                person.setSwerve(td);
                break;
            default:
                break;
        }
    }

    private void processRow2Switch8To14(int i, Person person, String td){
        switch (i){
            case 8:
                person.setHeader(td);
                break;
            case 9:
                person.setDefensive(td);
                break;
            case 10:
                person.setBallwinning(td);
                break;
            case 11:
                person.setKicking(td);
                break;
            case 12:
                person.setSpeed(td);
                break;
            case 13:
                person.setExplosive(td);
                break;
            case 14:
                person.setBody(td);
                break;
            default:
                break;
        }
    }

    private void processRow1(Element row1, Person person) {
        Elements trs = row1.select(TBODYTR);
        for(int i = 0; i < trs.size(); i++){
            String td = trs.get(i).select("td").last().text();
            if(i <= 7){
                processRow1Switch1To7(i, person, td);
            }else{
                processRow1SwitchAfter8(i, person, td, trs);
            }

        }
    }

    private void processRow1Switch1To7(int i, Person person, String td){
        switch (i){
            case 0:
                person.setName(td);
                break;
            case 1:
                person.setNumber(td);
                break;
            case 2:
                person.setTeamName(td);
                break;
            case 3:
                person.setLeague(td);
                break;
            case 4:
                person.setNationality(td);
                break;
            case 5:
                person.setRegion(td);
                break;
            case 6:
                person.setHeight(td);
                break;
            case 7:
                person.setWeight(td);
                break;
            default:
                break;
        }
    }

    private void processRow1SwitchAfter8(int i, Person person, String td, Elements trs){
        switch (i){
            case 8:
                person.setAge(td);
                break;
            case 9:
                person.setFoot(td);
                break;
            case 10:
                person.setCondition(td);
                break;
            case 11:
                person.setPosition(td);
                break;
            case 12:
                break;
            case 13:
                person.setPositionCan(processPosition(trs.get(i).select("td").last()));
                break;
            default:
                break;
        }
    }

    private Map<Integer, Set<String>> processPosition(Element td) {
        Element div1 = td.select("div.posFW").first();
        if(null != div1){
            processSpan(div1, 0);
            processSpan(div1, 1);
            processSpan(div1, 2);
        }
        Element div2 = td.select("div.posMF").first();
        if(null != div2){
            processSpan(div2, 0);
            processSpan(div2, 1);
            processSpan(div2, 2);
        }
        Element div3 = td.select("div.posDF").first();
        if(null != div3){
            processSpan(div3, 0);
            processSpan(div3, 1);
            processSpan(div3, 2);
        }
        Element div4 = td.select("div.posGK").first();
        if(null != div4){
            processSpan(div4, 0);
            processSpan(div4, 1);
            processSpan(div4, 2);
        }
        Map<Integer, Set<String>> map = new HashMap<>();
        map.put(0, pos0);
        map.put(1, pos1);
        map.put(2, pos2);
	    return map;
    }

    private void processSpan(Element div, Integer num){
	    String cssQuery = "span.pos" + num;
        Elements spans = div.select(cssQuery);
        if(spans != null && !spans.isEmpty()){
            if(num.equals(0)){
                pos0.addAll(spans.eachText());
            }else if(num.equals(1)){
                pos1.addAll(spans.eachText());
            }else if(num.equals(2)){
                pos2.addAll(spans.eachText());
            }
        }
    }

    private void processPage(){
        //加载第一页
        allPageList.add(baseUrl);
        Integer startPage = 2;
        Integer totalPage = 299;
        String url;
        for(int i = startPage; i < totalPage; i++){
            url = "http://pesdb.net/pes2018/?page=" + i;
            allPageList.add(url);
        }
    }

    private void getPlayerPageList(Document doc) {
        Element table = doc.select("table.players").first();
        Elements links = table.select("a[href]");
        for(Element link: links){
            String linkHref = link.attr("href");
            if(linkHref.contains("./?id=")){
                String newUrl = baseUrl + linkHref.replaceFirst(".", "");
                playerPageList.add(newUrl);
            }
        }
    }

	/**
	 * 导出为Excel
	 * @param list 列表
	 */
	private void exportToExcel(List<Person> list) {
		FileOutputStream out;
		String folderPath = "d://football" + File.separator;
		try {
			File folder = new File(folderPath);
			if (!folder.exists()) {
				Boolean isCreateDir = folder.mkdirs();
				log.info(isCreateDir.toString());
			}

			String fileName = System.currentTimeMillis() + ".xls";
			File file = new File(folderPath, fileName);
			if (!file.exists()) {
                Boolean isCreateFile = file.createNewFile();
				log.info(isCreateFile.toString());
			}

			out = new FileOutputStream(file.getPath());
			HSSFWorkbook workbook = new HSSFWorkbook();
			HSSFSheet sheet = workbook.createSheet("sheet1");
			HSSFRow row;
			HSSFCell cell;
			sheet.createFreezePane(0, 1);
			HSSFFont font = workbook.createFont();
			font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 
			HSSFCellStyle style = workbook.createCellStyle();
			style.setAlignment(HSSFCellStyle.ALIGN_CENTER); 
			style.setFont(font);
			style.setWrapText(true); 
            String[] title = {Constants.ID, "Player Name", "Team Name", "League", "Nationality", "Region", "Height", "Weight",
                    "Age", "Foot", "Position", "pos", "Attacking Prowess", "Ball Control", "Dribbling", "Low Pass",
                    "Lofted Pass", "Finishing", "Place Kicking", "Swerve", "Header", "Defensive Prowess",
                    "Ball Winning", "Kicking Power", "Speed", "Explosive Power", "Body Balance", "Jump", "Stamina",
                    "Goalkeeping", "Catching", "Clearing", "Reflexes", "Coverage", "Form", "Injury Resistance",
                    "Weak Foot Usage", "Weak Foot Accuracy", "Overall Rating", "Playing Style", "Player Skills", "COM Playing Style", "Rating as"};
			row = sheet.createRow(0);
			for(int i = 0; i < title.length; i++){
                cell = row.createCell(i);
                cell.setCellValue(title[i]);
            }
            int i = 1;
			for (Person person: list) {
				row = sheet.createRow(i++);
                cell = row.createCell(0);
                cell.setCellValue(person.getId());
                cell = row.createCell(1);
                cell.setCellValue(person.getName());
                cell = row.createCell(2);
                cell.setCellValue(person.getTeamName());
                cell = row.createCell(3);
                cell.setCellValue(person.getLeague());
                cell = row.createCell(4);
                cell.setCellValue(person.getNationality());
                cell = row.createCell(5);
                cell.setCellValue(person.getRegion());
                cell = row.createCell(6);
                cell.setCellValue(person.getHeight());
                cell = row.createCell(7);
                cell.setCellValue(person.getWeight());
                cell = row.createCell(8);
                cell.setCellValue(person.getAge());
                cell = row.createCell(9);
                cell.setCellValue(person.getFoot());
                cell = row.createCell(10);
                cell.setCellValue(person.getPosition());
                cell = row.createCell(11);
                cell.setCellValue(processPositionMapData(person.getPositionCan()));
                cell = row.createCell(12);
                cell.setCellValue(person.getAttacking());
                cell = row.createCell(13);
                cell.setCellValue(person.getBallcontrol());
                cell = row.createCell(14);
                cell.setCellValue(person.getDribbling());
                cell = row.createCell(15);
                cell.setCellValue(person.getLow());
                cell = row.createCell(16);
                cell.setCellValue(person.getLofted());
                cell = row.createCell(17);
                cell.setCellValue(person.getFinishing());
                cell = row.createCell(18);
                cell.setCellValue(person.getPlace());
                cell = row.createCell(19);
                cell.setCellValue(person.getSwerve());
                cell = row.createCell(20);
                cell.setCellValue(person.getHeader());
                cell = row.createCell(21);
                cell.setCellValue(person.getDefensive());
                cell = row.createCell(22);
                cell.setCellValue(person.getBallwinning());
                cell = row.createCell(23);
                cell.setCellValue(person.getKicking());
                cell = row.createCell(24);
                cell.setCellValue(person.getSpeed());
                cell = row.createCell(25);
                cell.setCellValue(person.getExplosive());
                cell = row.createCell(26);
                cell.setCellValue(person.getBody());
                cell = row.createCell(27);
                cell.setCellValue(person.getJump());
                cell = row.createCell(28);
                cell.setCellValue(person.getStamina());
                cell = row.createCell(29);
                cell.setCellValue(person.getGoalkeeping());
                cell = row.createCell(30);
                cell.setCellValue(person.getCatching());
                cell = row.createCell(31);
                cell.setCellValue(person.getClearing());
                cell = row.createCell(32);
                cell.setCellValue(person.getReflexes());
                cell = row.createCell(33);
                cell.setCellValue(person.getCoverage());
                cell = row.createCell(34);
                cell.setCellValue(person.getForm());
                cell = row.createCell(35);
                cell.setCellValue(person.getInjury());
                cell = row.createCell(36);
                cell.setCellValue(person.getWeakfootusage());
                cell = row.createCell(37);
                cell.setCellValue(person.getWeakfootaccuracy());
                cell = row.createCell(38);
                cell.setCellValue(person.getOverall());
                cell = row.createCell(39);
                cell.setCellValue(person.getStyle());
                cell = row.createCell(40);
                cell.setCellValue(person.getSkills());
                cell = row.createCell(41);
                cell.setCellValue(person.getCom());
                cell = row.createCell(42);
                cell.setCellValue(processRatingMapData(person.getRating()));
			}
			workbook.write(out);
            out.flush();
            out.close();
		} catch (IOException e) {
            log.error(e.getMessage());
            throw new MyException(ExceptionEnum.INPUTEXCEPTION, e);
		}
	}

	private String processRatingMapData(Map<String, String> map){
	    result = new StringBuilder();
        for(Map.Entry<String, String> vo : map.entrySet()){
            result.append(vo.getKey());
            result.append(Constants.COMMA);
            result.append(vo.getValue());
            result.append(";");
        }
        return result.toString();
    }

    private String processPositionMapData(Map<Integer, Set<String>> positionCan) {
	    result = new StringBuilder();
	    result.append("0:");
        foreachPosition(positionCan.get(0));
        result.append(";");
	    result.append("1:");
        foreachPosition(positionCan.get(1));
        result.append(";");
        result.append("2:");
        foreachPosition(positionCan.get(2));
	    return result.toString();
    }

    private void foreachPosition(Set<String> position){
        for(String pos: position){
            result.append(pos);
            result.append(Constants.COMMA);
        }
    }

    @Setter
    @Getter
	private class Person{
	    protected String id;
	    //basic info
        protected String name;
        protected String number;
        protected String teamName;
        protected String league;
        protected String nationality;
        protected String region;
        protected String height;
        protected String weight;
        protected String age;
        protected String foot;
        protected String condition;
        protected String position;
        protected Map<Integer, Set<String>> positionCan;
        //skill one row
        protected String attacking;
        protected String ballcontrol;
        protected String dribbling;
        protected String low;
        protected String lofted;
        protected String finishing;
        protected String place;
        protected String swerve;
        protected String header;
        protected String defensive;
        protected String ballwinning;
        protected String kicking;
        protected String speed;
        protected String explosive;
        protected String body;
        //skill two row
        protected String physical;
        protected String jump;
        protected String stamina;
        protected String goalkeeping;
        protected String catching;
        protected String clearing;
        protected String reflexes;
        protected String coverage;
        protected String form;
        protected String injury;
        protected String weakfootusage;
        protected String weakfootaccuracy;
        protected String overall;
        protected Map<String, String> rating;
        //playing style
        protected String style;
        protected String skills;
        protected String com;
    }

    /**
     * 根据图像地址获取图像到本地
     * @param urlString url地址
     * @param name 名称
     * @return 是否成功
     */
    public static boolean processImage(String urlString, String name){
        try{
            //new一个URL对象
            URL url = new URL(urlString);
            //打开链接
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            //设置请求方式为"GET"
            conn.setRequestMethod("GET");
            //超时响应时间为5秒
            conn.setConnectTimeout(5 * 1000);
            if(conn.getResponseCode() == 200){
                //通过输入流获取图片数据
                InputStream inStream = conn.getInputStream();
                //得到图片的二进制数据，以二进制封装得到数据，具有通用性
                byte[] data = readInputStream(inStream);
                //new一个文件对象用来保存图片，默认保存当前工程根目录
                File imageFile = new File("d:\\head\\" + name + ".png");
                //创建输出流
                FileOutputStream outStream = new FileOutputStream(imageFile);
                //写入数据
                outStream.write(data);
                //关闭输出流
                outStream.close();
                return true;
            }
        }catch (IOException e){
            log.error(e.getMessage());
            throw new MyException(ExceptionEnum.INPUTEXCEPTION, e);
        }
        return false;
    }

    private static byte[] readInputStream(InputStream inStream) throws IOException{
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        //创建一个Buffer字符串
        byte[] buffer = new byte[1024];
        //每次读取的字符串长度，如果为-1，代表全部读取完毕
        int len;
        //使用一个输入流从buffer里把数据读取出来
        while( (len=inStream.read(buffer)) != -1 ){
            //用输出流往buffer里写入数据，中间参数代表从哪个位置开始读，len代表读取的长度
            outStream.write(buffer, 0, len);
        }
        //关闭输入流
        inStream.close();
        //把outStream里的数据写入内存
        return outStream.toByteArray();
    }

    public static void main(String[] args) {
        SpiderUtil spiderUtil = new SpiderUtil();
        log.info(spiderUtil.baseUrl);
    }
}
